import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_shipper/SendGoods/TMSOrlder/AllTMSOrderList.dart';
import 'package:sph_shipper/SendGoods/TMSOrlder/DispatchedList.dart';
import 'package:sph_shipper/SendGoods/TMSOrlder/InvalidList.dart';
import 'package:sph_shipper/SendGoods/TMSOrlder/NoSchedulingList.dart';

import '../AppConfig/CityLIstData.dart';
import '../AppConfig/Config.dart';
import '../AppConfig/RouteAct.dart';
import '../Main/MyWallet/DepositManagement.dart';
import 'AddGoodsFirst.dart';
import 'TMSGoodsAddView.dart';
import 'TMSOrlder/DispatchList.dart';

class TMSOrderList extends StatefulWidget {
  const TMSOrderList({super.key});

  @override
  State<TMSOrderList> createState() => _TMSOrderListState();
}

class _TMSOrderListState extends State<TMSOrderList> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  late int indexPage;

  @override
  void initState() {
  super.initState();
// 初始化TabController，并指定初始索引和长度
  _tabController = TabController(initialIndex: 0, length: 3, vsync: this);

  }
  @override
  void dispose() {
// 销毁TabController
  _tabController.dispose();
  super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        backgroundColor:Colors.white,
        centerTitle: true,
        title: Text("订单管理",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,fontSize: 16.sp),),
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),

        bottom: PreferredSize(
          preferredSize: Size.fromHeight(48.0), // 设置TabBar的高度
          child: Container(
            alignment: Alignment.centerLeft,
            child:Column(
              children: [
                TabBar(
                  padding: EdgeInsets.only(left: 0),
                  isScrollable: false,
                  labelPadding: EdgeInsets.symmetric(horizontal: 0.0.w),
                  controller: _tabController,
                  tabs: [
                    Tab(child:
                    Container(
                      width: 55.w,
                      height: 20.h,
                      child:  Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("待调度",style: TextStyle(fontSize: 13.sp),),
                          // Badge(label: Text("1"),),
                        ],
                      ),

                    )
                    ),

                    Tab(child:
                    Container(
                      width: 55.w,
                      height: 20.h,
                      child:  Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("已调度",style: TextStyle(fontSize: 13.sp),),
                          // Badge(label: Text("1"),),
                        ],
                      ),

                    )
                    ),
                    Tab(child:
                    Container(
                      width: 55.w,
                      height: 20.h,
                      child:  Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("已作废",style: TextStyle(fontSize: 13.sp),),
                          // Badge(label: Text("1"),),
                        ],
                      ),
                    ),
                    ),
                  ],
                  labelColor: Color.fromRGBO(231, 112, 27, 0.85),
                  dividerColor: Colors.white,
                  unselectedLabelColor: Colors.black,
                  indicatorColor:Color.fromRGBO(231, 112, 27, 0.85),
                ),

              ],
            ) ,

          ),
        )
      ),


        body:
        TabBarView(
// 绑定控制器
          controller: _tabController,
          children: [
            NoSchedulingList(),
            DispatchedList(),
            InvalidList(),
          ],

        )
    );
  }
}
class TMSFiltering extends StatefulWidget {
  const TMSFiltering({super.key});

  @override
  State<TMSFiltering> createState() => _TMSFilteringState();
}

class _TMSFilteringState extends State<TMSFiltering> {
  String timerStr="选择日期范围";
  String timerStr2="选择日期范围";
  List schedulingList=['平台找车',"自有车","承运商","外调车"];
  String schedulingStr="";
  double _startX=0;
  String customerName="";
  String startCitynameOne="";
  List startCitycodeOne=[];
  List startAddressOne=[];
  String endCitynameOne="";
  List endCitycodeOne=[];
  List endAddressOne=[];
  int schedulingIndex=-1;
  List scheduNameList=["王二二(18888888888)","王二二(18888888888)","王二二(18888888888)","王二二(18888888888)","王二二(18888888888)","王二二(18888888888)"];
  String picType="";
  String trackingNumber="";
  String deliveryNumber="";
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return  GestureDetector(
        onHorizontalDragStart: (details) {
      _startX = details.globalPosition.dx;
    },
    onHorizontalDragUpdate: (details) {
    // Calculate the distance moved
    double deltaX = details.globalPosition.dx - _startX;
    if (deltaX > 50) {
    Navigator.of(context).pop();
    }
    },
    child:
      Drawer(
      width: screenSize.width,
      child: Row(
        children: [
          GestureDetector(
            child:   Container(
              width: 40.w,
              height: screenSize.height,
              color: MyColors.backgreyColor,
            ),
            onTap: (){
              Navigator.pop(context);
            },
          ),
          Container(
            color: Colors.white,
            padding: EdgeInsets.only(top: 50.h,left: 10.w,right: 10.w,bottom: 30.h),
          child:Column(
            children: [
              Expanded(child:
              Column(
              crossAxisAlignment: CrossAxisAlignment.start,
                children: [

                  Text("创建时间",style: TextStyle(color:Colors.black,fontSize: 14.sp),),
                  SizedBox(height: 10.h,),
                  BrnSmallOutlineButton(
                    title: timerStr2,
                    lineColor: MyColors.authTipColor,
                    textColor:timerStr2=="选择日期范围"? MyColors.authTipColor:MyColors.textColor,
                    fontSize: 13.sp,
                    width: screenSize.width-100.w,
                    radius: 10,
                    onTap: () async{
                      final result= await showModalBottomSheet(
                        context: context,
                        builder: (BuildContext context) {
                          return dataTimeSheet(type: 1,
                          );
                        },
                      );
                      if (result != null) {

                        setState(() {
                          timerStr2=result;
                        });
                      }
                    },
                  ),
                  SizedBox(height: 10.h,),


                  GestureDetector(
                    child: Container(
                      margin: EdgeInsets.only(top: 10.h),
                      width: screenSize.width-60.w,
                      child:  Row(

                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text("客户名称",style: TextStyle(color:Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              customerName==""?
                              Text("请输入",style: TextStyle(color: MyColors.hintColor,fontSize: 14.sp),)
                              :Text(customerName,style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                    ),
                    onTap: (){
                      showModalBottomSheet(
                        context: context,
                        isDismissible: false,
                        builder: (BuildContext context) {
                          return Container(
                              padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 15.h),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(6)),
                              color: Colors.white,
                            ),
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    GestureDetector(
                                      child: Text("取消",style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                                      onTap: (){
                                        setState(() {
                                          customerName="";
                                        });

                                        Navigator.pop(context);
                                      },
                                    ),
                                    Text("客户名称",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w600),),
                                    GestureDetector(
                                      child:Text("确定",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                                      onTap: (){
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                                SizedBox(height: 10.h,),
                                Container(
                                  height: 35.h,
                                  width: screenSize.width-40.w,
                                  alignment: Alignment.centerLeft,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(6)),
                                    color: MyColors.backgreyColor,
                                  ),
                                  child:TextField(
                                    style: TextStyle(color: Colors.black,fontSize: 14.sp),
                                    keyboardType: TextInputType.number,
                                    decoration: InputDecoration(
                                      filled: true,
                                      fillColor: Color.fromRGBO(248, 248, 248, 1),
                                      hintText: "请输入客户名称",
                                      //边框
                                      focusedBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color: Color.fromRGBO(248, 248, 248, 1))),
                                      enabledBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color:Color.fromRGBO(248, 248, 248, 1),)),
                                      hintStyle: TextStyle(
                                        color: Color.fromRGBO(183, 183, 183, 1),
                                        fontSize:  MyFontSizes.commonrText.sp,
                                      ),
                                    ),
                                    onChanged: (value) {
                                      setState(() {
                                        customerName=value;
                                      });
                                    },

                                  ),
                                ),
                              ],
                            ),

                          );
                        },
                      );
                    },

                  ),
                  Divider(height: 16.h,color: MyColors.backgreyColor,),
                  GestureDetector(
                    child: Container(

                      width: screenSize.width-60.w,
                      child:  Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [

                          Text("客户订单号",style: TextStyle(color:Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              trackingNumber==""?
                              Text("请输入",style: TextStyle(color: MyColors.hintColor,fontSize: 14.sp),)
                                  : Text(trackingNumber,style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                    ),
                    onTap: (){
                      showModalBottomSheet(
                        context: context,
                        isDismissible: false,
                        builder: (BuildContext context) {
                          return Container(
                            padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 15.h),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(6)),
                              color: Colors.white,
                            ),
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    GestureDetector(
                                      child: Text("取消",style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                                      onTap: (){
                                        setState(() {
                                          trackingNumber="";
                                        });

                                        Navigator.pop(context);
                                      },
                                    ),
                                    Text("客户单号",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w600),),
                                    GestureDetector(
                                      child:Text("确定",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                                      onTap: (){
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                                SizedBox(height: 10.h,),
                                Container(
                                  height: 35.h,
                                  width: screenSize.width-40.w,
                                  alignment: Alignment.centerLeft,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(6)),
                                    color: MyColors.backgreyColor,
                                  ),
                                  child:TextField(
                                    style: TextStyle(color: Colors.black,fontSize: 14.sp),
                                    keyboardType: TextInputType.number,
                                    decoration: InputDecoration(
                                      filled: true,
                                      fillColor: Color.fromRGBO(248, 248, 248, 1),
                                      hintText: "请输入客户单号",
                                      //边框
                                      focusedBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color: Color.fromRGBO(248, 248, 248, 1))),
                                      enabledBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color:Color.fromRGBO(248, 248, 248, 1),)),
                                      hintStyle: TextStyle(
                                        color: Color.fromRGBO(183, 183, 183, 1),
                                        fontSize:  MyFontSizes.commonrText.sp,
                                      ),
                                    ),
                                    onChanged: (value) {
                                      setState(() {
                                        trackingNumber=value;
                                      });
                                    },

                                  ),
                                ),
                              ],
                            ),

                          );
                        },
                      );
                    },
                  ),
                  Divider(height: 16.h,color: MyColors.backgreyColor,),
                  GestureDetector(
                    child: Container(

                      width: screenSize.width-60.w,
                      child:  Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [

                          Text("客户提货单号",style: TextStyle(color:Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              deliveryNumber==""?
                              Text("请输入",style: TextStyle(color: MyColors.hintColor,fontSize: 14.sp),)
                                  : Text(deliveryNumber,style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                    ),
                    onTap: (){
                      showModalBottomSheet(
                        context: context,
                        isDismissible: false,
                        builder: (BuildContext context) {
                          return Container(
                            padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 15.h),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(6)),
                              color: Colors.white,
                            ),
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    GestureDetector(
                                      child: Text("取消",style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                                      onTap: (){
                                        setState(() {
                                          deliveryNumber="";
                                        });

                                        Navigator.pop(context);
                                      },
                                    ),
                                    Text("客户提货单号",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w600),),
                                    GestureDetector(
                                      child:Text("确定",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                                      onTap: (){
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                                SizedBox(height: 10.h,),
                                Container(
                                  height: 35.h,
                                  width: screenSize.width-40.w,
                                  alignment: Alignment.centerLeft,
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.all(Radius.circular(6)),
                                    color: MyColors.backgreyColor,
                                  ),
                                  child:TextField(
                                    style: TextStyle(color: Colors.black,fontSize: 14.sp),
                                    keyboardType: TextInputType.number,
                                    decoration: InputDecoration(
                                      filled: true,
                                      fillColor: Color.fromRGBO(248, 248, 248, 1),
                                      hintText: "请输入客户单号",
                                      //边框
                                      focusedBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color: Color.fromRGBO(248, 248, 248, 1))),
                                      enabledBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color:Color.fromRGBO(248, 248, 248, 1),)),
                                      hintStyle: TextStyle(
                                        color: Color.fromRGBO(183, 183, 183, 1),
                                        fontSize:  MyFontSizes.commonrText.sp,
                                      ),
                                    ),
                                    onChanged: (value) {
                                      setState(() {
                                        deliveryNumber=value;
                                      });
                                    },

                                  ),
                                ),
                              ],
                            ),

                          );
                        },
                      );
                    },
                  ),
                  Divider(height: 16.h,color: MyColors.backgreyColor,),
                  GestureDetector(
                    child: Container(

                      width: screenSize.width-60.w,
                      child:  Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text("调度员",style: TextStyle(color:Colors.black,fontSize: 14.sp),),
                          Row(
                            children: [
                              schedulingIndex!=-1?
                              Text(scheduNameList[schedulingIndex],style: TextStyle(color: Colors.black,fontSize: 14.sp),):
                              Text("请选择",style: TextStyle(color: MyColors.hintColor,fontSize: 14.sp),),
                              Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 14,),
                            ],
                          )
                        ],
                      ),
                    ),
                   onTap: (){
                       showModalBottomSheet(
                           context: context,
                           builder: (BuildContext context) {
                             return Container(
                               width: screenSize.width,
                               padding: EdgeInsets.only(left: 10.w, right: 10.w, top: 15.h),
                               decoration: BoxDecoration(
                                 borderRadius: BorderRadius.all(Radius.circular(6)),
                                 color: Colors.white,
                               ),
                               child: Column(
                                 children: [
                                   Row(
                                     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                     children: [
                                       SizedBox(width: 30,),
                                       Text("请选择调度员",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w600),),
                                       GestureDetector(
                                         child: Icon(Icons.close,color: MyColors.textColor,size: 30,),
                                         onTap: (){Navigator.pop(context);},
                                       )
                                     ],
                                   ),
                                  Divider(height: 20.h,color: MyColors.backgreyColor,),
                                   Container(
                                       height: 35.h,
                                       margin: EdgeInsets.only(top: 8.h,bottom: 4.h,left: 30.w,right: 30.w),
                                       padding: EdgeInsets.only(left: 8.w),
                                       decoration: BoxDecoration(
                                           color:MyColors.backgreyColor,
                                           borderRadius: BorderRadius.all(Radius.circular(30))),
                                       child:Row(
                                           children: [
                                             Icon(Icons.search,color: MyColors.authTipColor,),
                                             Container(
                                               height: 35.h,
                                               width: 200.w,
                                               decoration: BoxDecoration(
                                                   borderRadius: BorderRadius.all(Radius.circular(30))),
                                               child:   TextField(
                                                 decoration: InputDecoration(
                                                   hintText: '请输入姓名或手机号',
                                                   hintStyle:TextStyle(fontSize: 14.sp,color: MyColors.textColor),
                                                   filled: true,
                                                   fillColor: MyColors.backgreyColor,
                                                   border: InputBorder.none,
                                                   alignLabelWithHint: true,
                                                   // contentPadding: EdgeInsets.symmetric(vertical: 10.0.h, horizontal: 10.0.w),
                                                 ),
                                                 textAlign: TextAlign.left,
                                                 style: TextStyle(color: Colors.black),
                                                 onChanged: (value) {
                                                   setState(() {


                                                   });
                                                 },
                                               ),
                                             ),

                                           ])
                                   ),
                                   Divider(height: 16.h,color: MyColors.backgreyColor,),
                                  Container(
                                    height: screenSize.height/2-100.h,
                                    child: ListView.builder(
                                      shrinkWrap:false,
                                      itemCount:scheduNameList.length,
                                      itemBuilder: (BuildContext context, int index) {
                                        return GestureDetector(
                                          child:
                                          Container(
                                            child: Column(
                                              children: [
                                                Row(
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  children: [
                                                    Text("王二二(18888888888)",style: TextStyle(color:schedulingIndex==index?MyColors.themeColor: Colors.black,fontSize: 14.sp),),
                                                    Text("剩余三次",style: TextStyle(color: schedulingIndex==index?MyColors.themeColor:Colors.black,fontSize: 14.sp),),
                                                  ],
                                                ),

                                                Divider(height: 16.h,color: MyColors.backgreyColor,),
                                              ],
                                            ),
                                          ),
                                          onTap: (){
                                            setState(() {
                                              schedulingIndex=index;
                                              schedulingStr=scheduNameList[index];
                                            });
                                            Navigator.pop(context);

                                          },
                                        );
                                        },
                                  )
                                  ),
                                 ],
                               )
                             );
                           }
                       );

                   },
                  ),
                  Container(width: screenSize.width-60.w,height: 1.h,color: MyColors.backgreyColor,margin: EdgeInsets.only(top: 10.h),),
                  SizedBox(height: 30.h,),
                  Container(
                    height: 30.h,
                    child: Row(
                      children: [
                        BrnSmallOutlineButton(
                          title: '重置',
                          lineColor: MyColors.themeColor,
                          textColor: MyColors.themeColor,
                          fontSize: 14.sp,
                          width: (screenSize.width-70.w)/2,
                          radius: 30,
                          onTap: () {
                            setState(() {
                              timerStr="选择日期范围";
                              schedulingStr="";
                              customerName="";
                              trackingNumber="";
                              deliveryNumber="";
                            });
                          },
                        ),
                        SizedBox(width: 10.w,),
                        BrnSmallMainButton(
                          title: '确定',
                          bgColor: MyColors.themeColor,
                          textColor: Colors.white,
                          radius: 30,
                          fontSize: 14.sp,
                          width: (screenSize.width-70.w)/2,
                          onTap: () {
                            Navigator.pop(context,[timerStr,customerName,trackingNumber,deliveryNumber,schedulingStr]);
                          },
                        )
                      ],
                    ),
                  )
                ],
              ),
              ),


            ],
          ),
          )

        ],
      )
      )
    );
  }
}
